package co.edu.usbcali.prototipobeta.dataaccess.dao;

import co.edu.usbcali.prototipobeta.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.prototipobeta.modelo.Bebe;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * Bebe entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class BebeDAO implements IBebeDAO {
    private static final Log log = LogFactory.getLog(BebeDAO.class);

    // property constants
    //public static final Long  BEBEEDAD = "bebeEdad";
    public static final String BEBEEDAD = "bebeEdad";

    //public static final Date  BEBEFECHANACIMIENTO = "bebeFechanacimiento";
    public static final String BEBEFECHANACIMIENTO = "bebeFechanacimiento";

    //public static final Long  BEBEID = "bebeId";
    public static final String BEBEID = "bebeId";

    //public static final String  BEBENOMBRE = "bebeNombre";
    public static final String BEBENOMBRE = "bebeNombre";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Bebe Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Bebe instance) {
        log.debug("saving Bebe instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Bebe Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Bebe instance) {
        log.debug("deleting Bebe instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Bebe Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Bebe instance) {
        log.debug("updating Bebe instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Bebe findById(Long id) {
        log.debug("finding Bebe instance with id: " + id);

        try {
            Bebe instance = (Bebe) getSession().get(Bebe.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Bebe failed", re);
            throw re;
        }
    }

    public List<Bebe> findByExample(Bebe instance) {
        log.debug("finding Bebe instance by example");

        try {
            List results = getSession().createCriteria("Bebe")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Bebe entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Bebe> found by query
        */
    public List<Bebe> findByProperty(String propertyName, Object value) {
        log.debug("finding Bebe instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Bebe as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Bebe> findByBebeEdad(Object bebeEdad) {
        return findByProperty(BEBEEDAD, bebeEdad);
    }

    public List<Bebe> findByBebeFechanacimiento(Object bebeFechanacimiento) {
        return findByProperty(BEBEFECHANACIMIENTO, bebeFechanacimiento);
    }

    public List<Bebe> findByBebeId(Object bebeId) {
        return findByProperty(BEBEID, bebeId);
    }

    public List<Bebe> findByBebeNombre(Object bebeNombre) {
        return findByProperty(BEBENOMBRE, bebeNombre);
    }

    /**
    * Find all Bebe entities.
    *
    * @return List<Bebe> all Bebe instances
    */
    public List<Bebe> findAll() {
        log.debug("finding all Bebe instances");

        try {
            String queryString = "from Bebe";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Bebe> findByCriteria(String whereCondition) {
        log.debug("finding Bebe " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Bebe model " + where;
            Query query = getSession().createQuery(queryString);
            List<Bebe> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Bebe failed", re);
            throw re;
        }
    }

    public List<Bebe> findPageBebe(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Bebe findPageBebe");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Bebe model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Bebe model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberBebe() {
        log.debug("finding Bebe count");

        try {
            String queryString = "select count(*) from Bebe model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    
    public Bebe consultarBebePorUsuarioID(Long usuID){
    	Bebe bebe = null;
    	List<Bebe> listabebes = null;
       try {
    	   
    	   String queryString = "select b from Bebe b where(b.usuario.usuId=:usu)";
           Query queryObject = getSession().createQuery(queryString);
           queryObject.setParameter("usu", usuID);           
           listabebes = queryObject.list();
           if(listabebes.size() !=0){
           bebe= listabebes.get(0);
           }
           return bebe;
        } catch (RuntimeException re) {
           throw re;
        }
    }
    
    
}
